package com.test.processor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TimingBeanPostProcessor implements BeanPostProcessor{
    private Map<String, Long> startTimes = new ConcurrentHashMap<>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws Exception {
        startTimes.put(beanName, System.currentTimeMillis());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws Exception {
        Long startTime = startTimes.remove(beanName);
        if (startTime != null){
            System.out.printf("bean [%s] 初始化耗时 %d ms%n", beanName, System.currentTimeMillis() - startTime);
        }
        return bean;
    }
}
